// 引入路由器
import store from "@/store";
import { createRouter, createWebHistory } from "vue-router";

const routes = [
    {
        path: '/login', name: "login", component: () => import("@/views/pages/login.vue")
    },
    {
        path: '/', name: "layout", component: () => import("@/views/layout/layout.vue"),
        redirect: "/login",
    },
    {
        path: '/main', name: "main", component: () => import("@/views/layout/layout.vue"),
        children: [
            {
                path: '/index', name: "index", component: () => import("@/views/pages/index.vue"),
            },
            {
                path: '/content', name: "content", component: () => import("@/views/pages/content.vue"),
            }
        ]
    }
]

const router = createRouter({
    // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    history: createWebHistory(),
    routes, // `routes: routes` 的缩写
})

router.beforeEach((to, form, next) => {

    const userInfo = store.state.user.userInfo
    console.info(userInfo)
    if (!userInfo.userName) {
        if (to.path === "/login") {
            // userInfo1 = store.state.user.userInfo
            // if (!userInfo1.userName) {
                next()
                return;
            // }else{
            //     next("/main")
            // }
        }
        next("/login")
    } else {
        next()
    }
})
export default router;